package com.jiayi.weiphoto.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Thread.State;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import com.jiayi.weiphoto.entity.ImageEntity;



import android.graphics.Bitmap;
import android.os.Environment;
import android.os.Handler;

public class SearchSyncImageLoader {

	private Object lock = new Object();

	private boolean mAllowLoad = true;

	private boolean firstLoad = true;

	private int mStartLoadLimit = 0;

	private int mStopLoadLimit = 0;

	final Handler handler = new Handler();
	
	String filePath = "";

	private HashMap<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();

//	RunInOtherThread runInOutherThread;
	
	static SearchSyncImageLoader imageLoader;
	
	public static SearchSyncImageLoader getInstance(){
		if(imageLoader == null){
			imageLoader = new SearchSyncImageLoader();
		}
		return imageLoader;
	}
	
	public void setCacheRootPath(String path){
		this.filePath = path;
	}
	
	private SearchSyncImageLoader(){
		
	}

	private SearchSyncImageLoader(String fileRoot) {
		this.filePath = fileRoot;
	}

	public interface OnImageLoadListener {
		public void onImageLoad(String iconUrl, Bitmap bitmap, int pos);

		public void onError(Integer t);
	}

	public void setLoadLimit(int startLoadLimit, int stopLoadLimit) {
		if (startLoadLimit > stopLoadLimit) {
			return;
		}
		mStartLoadLimit = startLoadLimit;
		mStopLoadLimit = stopLoadLimit;
	}

	public void restore() {
		mAllowLoad = true;
		firstLoad = true;
	}

	public void lock() {
		mAllowLoad = false;
		firstLoad = false;
	}

	public void unlock() {
		mAllowLoad = true;
		synchronized (lock) {
			lock.notifyAll();
		}
	}
	
	public void finishLoadingImg(){
	}

	public void loadImage(Integer t, String imageUrl,
			OnImageLoadListener listener) {
		final OnImageLoadListener mListener = listener;
		final String mImageUrl = imageUrl;
		final Integer mt = t;
		new Thread(new Runnable() {

			@Override
			public void run() {
				MyLog.e("loadImage is ===>"+mImageUrl);
				loadImage(mImageUrl, mt, mListener);
			}
		}).start();
	}
	
	private void loadImage(final String mImageUrl, final Integer mt,
			final OnImageLoadListener mListener) {
		if(mImageUrl == null || mImageUrl.length() == 0) return ;
		if (imageCache.containsKey(mImageUrl)) {
			SoftReference<Bitmap> softReference = imageCache.get(mImageUrl);
			final Bitmap d = softReference.get();
			if (d != null) {
				handler.post(new Runnable() {
					@Override
					public void run() {
						if (mAllowLoad) {
							if(mListener != null){
								mListener.onImageLoad(mImageUrl, d, mt);
							}
						}
					}
				});
				return;
			}
		}
		final Bitmap d = HttpUtil.getBitmap(mImageUrl, filePath);
		if (d != null) {
			imageCache.put(mImageUrl, new SoftReference<Bitmap>(d));
		}
		handler.post(new Runnable() {
			@Override
			public void run() {
				if (mAllowLoad) {
					if(mListener != null){
						mListener.onImageLoad(mImageUrl, d, mt);
					}
				}
			}
		});
	}

	public void dump() {
		// TODO Auto-generated method stub
		for(String key : imageCache.keySet()){
			if(imageCache.get(key).get() != null && !imageCache.get(key).get().isRecycled()){
				imageCache.get(key).get().recycle();
				imageCache.get(key).clear();
			}
		}
		imageCache.clear();
	}
}

